package CtrLayer;
import java.util.ArrayList;

import DBLayer.*;
import ModelLayer.*;

public class ProductCtr {
	
private static ProductCtr instance;
	
	private ProductCtr()
	{
		
	}
	
	public static ProductCtr getInstance()
    {
        if(instance==null)
        {
            instance = new ProductCtr();
        }
        return instance;
    }
	
	public ArrayList<Product> findAllProducts()
	{
		IFDBProduct dbProd = new DBProduct();
	    ArrayList<Product> allProd = new ArrayList<Product>();
	    allProd = dbProd.getAllProducts(false);
	    return allProd;
	}
	
	
	
	public Product findByName(String name)
    {
        IFDBProduct dbProd = new DBProduct();
        return dbProd.searchProductName(name, true);
    }
	
	public Product findByDescription(String name)
    {
        IFDBProduct dbProd = new DBProduct();
        return dbProd.searchProductDescription(name, true);
    }
	
	public Product findProduct(int prodId)
    {
        IFDBProduct dbProd = new DBProduct();
        return dbProd.findProduct(prodId, true);
    }
	
	public int deleteByProdId(int prodId)
    {
        IFDBProduct dbProd = new DBProduct();
        return dbProd.deleteProduct(prodId);
    }
	
	public int updateClothing(int productId, String name, double purchasePrice, double salesPrice, double rentPrice, String countryOfOrigin, Range rangeID, String description, String clothingSize, String clothingColor, Supplier supplierID)
    {
        IFDBProduct dbProd = new DBProduct();
        Clothing clo = new Clothing();
        clo.setProductId(productId);
        clo.setName(name);
        clo.setPurchasePrice(purchasePrice);
        clo.setSalesPrice(salesPrice);
        clo.setRentPrice(rentPrice);
        clo.setCountryOfOrigin(countryOfOrigin);
        clo.setRange(rangeID);
        clo.setDescription(description);
        clo.setClothingSize(clothingSize);
        clo.setClothingColor(clothingColor);
        clo.setSupplier(supplierID);
        return  dbProd.updateClothing(clo);
    }
	
	public int updateEquipment(int productId, String name, double purchasePrice, double salesPrice, double rentPrice, String countryOfOrigin, Range rangeID, String description, String equipmentType, Supplier supplierID)
    {
        IFDBProduct dbProd = new DBProduct();
        Equipment equ = new Equipment();
        equ.setProductId(productId);
        equ.setName(name);
        equ.setPurchasePrice(purchasePrice);
        equ.setSalesPrice(salesPrice);
        equ.setRentPrice(rentPrice);
        equ.setCountryOfOrigin(countryOfOrigin);
        equ.setRange(rangeID);
        equ.setDescription(description);
        equ.setEquipmentType(equipmentType);
        equ.setSupplier(supplierID);
        return  dbProd.updateEquipment(equ);
    }
	
	public int updateGun(int productId, String name, double purchasePrice, double salesPrice, double rentPrice, String countryOfOrigin, Range rangeID, String description, String gunFabricator, String gunCalibre, Supplier supplierID)
    {
        IFDBProduct dbProd = new DBProduct();
        Gun gun = new Gun();
        gun.setProductId(productId);
        gun.setName(name);
        gun.setPurchasePrice(purchasePrice);
        gun.setSalesPrice(salesPrice);
        gun.setRentPrice(rentPrice);
        gun.setCountryOfOrigin(countryOfOrigin);
        gun.setRange(rangeID);
        gun.setDescription(description);
        gun.setGunFabric(gunFabricator);
        gun.setGunCalibre(gunCalibre);
        gun.setSupplier(supplierID);
        return  dbProd.updateGun(gun);
    }
	
	public int updateProduct(int productId, String name, double purchasePrice, double salesPrice, double rentPrice, String countryOfOrigin, Range rangeID, String description, Supplier supplierID)
	{
		IFDBProduct dbProd = new DBProduct();
        Product pro = new Product();
        pro.setProductId(productId);
        pro.setName(name);
        pro.setPurchasePrice(purchasePrice);
        pro.setSalesPrice(salesPrice);
        pro.setRentPrice(rentPrice);
        pro.setCountryOfOrigin(countryOfOrigin);
        pro.setRange(rangeID);
        pro.setDescription(description);
       
        pro.setSupplier(supplierID);
        return  dbProd.updateProduct(pro);
	}
	
	public void insertClothing(int productId, String name, double purchasePrice, double salesPrice, double rentPrice, String countryOfOrigin, int rangeID, String description, String clothingSize, String clothingColor, int supplierID)
    {    
         Clothing clo = new Clothing( productId,  name,  purchasePrice,  salesPrice,  rentPrice,  countryOfOrigin,  rangeID,  description,  clothingSize,  clothingColor,  supplierID);
         System.out.println("ProductCtr - new Clothing() result: "+clo);
         clo.setProductId(productId);
         clo.setName(name);
         clo.setPurchasePrice(purchasePrice);
         clo.setSalesPrice(salesPrice);
         clo.setRentPrice(rentPrice);
         clo.setCountryOfOrigin(countryOfOrigin);
       // Range rngObj = clo.getRange(); //already created
         System.out.println("ProductCtr - clo.getRange(); "+clo.getRange());
         //rngObj.setRangeID(rangeID); //id already set
         clo.setDescription(description);
         clo.setClothingSize(clothingSize);
         clo.setClothingColor(clothingColor);
         //clo.getSupplier().setSupplierId(supplierID); // supplier already set
         System.out.println("ProductCtr - clo.getSupplier(); "+clo.getSupplier());
         
         
         
         ///////////////// fra company code ...
//         try{
//             DbConnection1.startTransaction();
//             DBEmployee dbEmp = new DBEmployee();
//             dbEmp.insertEmployee(empObj);
//             DbConnection1.commitTransaction();
//            }
         ////////////////
  
         try{
          DbConnection1.startTransaction();
          DBProduct dbProd = new DBProduct();
          System.out.println("ProductCtr - new DBProduct(); "+dbProd);
          dbProd.insertClothing(clo);
          DbConnection1.commitTransaction();
         }
         catch(Exception e)
         {
             DbConnection1.rollbackTransaction();
         }
    }
	
	public void insertEquipment(int productId, String name, double purchasePrice, double salesPrice, double rentPrice, String countryOfOrigin, int rangeID, String description, String equipmentType, int supplierID)
    {    
		Equipment equ = new Equipment(productId, name, purchasePrice, salesPrice, rentPrice, countryOfOrigin, rangeID, description, equipmentType,  supplierID );
        System.out.println("ProductCtr - new Equipment(productId, " +
        		"name, purchasePrice, salesPrice, rentPrice, " +
        		"countryOfOrigin, rangeID, description, equipmentType,  " +
        		"supplierID ); "+equ); //this new equipcall needed inputparameters!
		equ.setProductId(productId);
        equ.setName(name);
        equ.setPurchasePrice(purchasePrice);
        equ.setSalesPrice(salesPrice);
        equ.setRentPrice(rentPrice);
        equ.setCountryOfOrigin(countryOfOrigin);
        System.out.println("ProductGUI - equ.setCountryOfOrigin(countryOfOrigin); " + equ.getCountryOfOrigin());
        Range ran = equ.getRange();
        System.out.println("ProductCtr - equ.getRange(); "+equ.getRange());
        //ran.setRangeID(rangeID);  // rangeid already set                                                              //stopper her.. 
        //System.out.println("ProductGUI - ran.setRangeID(rangeID); "+ran.getRangeID());//stopper her..
        equ.setDescription(description);
        //System.out.println("ProductGUI - equ.setDescription(description); " + equ.getCountryOfOrigin());
        equ.setEquipmentType(equipmentType);  
        //System.out.println("ProductGUI - equ.setEquipmentType(equipmentType); " + equ.getCountryOfOrigin());
        //equ.getSupplier().setSupplierId(supplierID);//supplier already set
        System.out.println("ProductGUI - equ.getSupplier();"+equ.getSupplier());
  
///////////////// fra company code ...
// try{
//     DbConnection1.startTransaction();
//     DBEmployee dbEmp = new DBEmployee();
//     dbEmp.insertEmployee(empObj);
//     DbConnection1.commitTransaction();
//    }
 ////////////////
        
         try{
          DbConnection1.startTransaction();
          DBProduct dbProd = new DBProduct();
          dbProd.insertEquipment(equ);
          DbConnection1.commitTransaction();
         }
         catch(Exception e)
         {
             DbConnection1.rollbackTransaction();
         }
    }
	
	public void insertGun(int productId, String name, double purchasePrice, double salesPrice, double rentPrice, String countryOfOrigin, int rangeID, String description, String gunFabricator, String gunCalibre, int supplierID)
    {    
		Gun gun = new Gun( productId,  name,  purchasePrice,  salesPrice,  rentPrice,  countryOfOrigin,  rangeID,  description,  gunFabricator,  gunCalibre,  supplierID); // needed inputparameters
        gun.setProductId(productId);
        gun.setName(name);
        gun.setPurchasePrice(purchasePrice);
        gun.setSalesPrice(salesPrice);
        gun.setRentPrice(rentPrice);
        gun.setCountryOfOrigin(countryOfOrigin);
        gun.getRange().setRangeID(rangeID);
        gun.setDescription(description);
        gun.setGunFabric(gunFabricator);
        gun.setGunCalibre(gunCalibre);
        gun.getSupplier().setSupplierId(supplierID);
        System.out.println("ProductCtr - gun.getRange() and gun.getSupplier() "+gun.getRange() + " " +gun.getSupplier());
  
        
///////////////// fra company code ...
// try{
//     DbConnection1.startTransaction();
//     DBEmployee dbEmp = new DBEmployee();
//     dbEmp.insertEmployee(empObj);
//     DbConnection1.commitTransaction();
//    }
 ////////////////
        
         try{
          DbConnection1.startTransaction();
          DBProduct dbProd = new DBProduct();
          dbProd.insertGun(gun);
          DbConnection1.commitTransaction();
         }
         catch(Exception e)
         {
             DbConnection1.rollbackTransaction();
         }
    }

}
